package i18n

import (
	"github.com/gin-gonic/gin"
	"github.com/kortem/lingo"
)

const TRANS_PATH = "common/middleware/i18n/translations"

func SetI18n() gin.HandlerFunc {
	return func(c *gin.Context) {
		SetT(c)

		c.Next()
	}
}

func GetT(c *gin.Context) *lingo.T {
	t, exists := c.Get("T")
	if exists {
		return t.(*lingo.T)
	} else {
		return SetT(c)
	}
}

func SetT(c *gin.Context) *lingo.T {
	l := lingo.New("en_US", TRANS_PATH)
	T := l.TranslationsForRequest(c.Request)

	c.Set("T", T)

	return &T
}
